<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/9/17 0017
 * Time: 下午 9:13
 */

namespace Home\Controller;


use Think\Controller;

class BindController extends Controller
{
    /**
     * 绑定手机
     */
    public function index()
    {
        //echo '1111111111111'.session('openid');exit;
        $this->display();
    }

    public function send()
    {
        $tel = I('tel');
        $CheckCode= rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9);
        //$res = $this->sendTemplateSMS($tel,array($CheckCode,'5'),"1");
        $treemember = D('treemember');
        $telres = $treemember->where(array('tel'=>$tel))->find();
        if ($telres == false) {
            $this->ajaxReturn(array('reg'=>3),'json');
        }
        $res = $this->sendVoiceSMS($CheckCode,"3",$tel,"106665384","营销外呼状态通知回调地址",'zh','第三方私有数据');
        if($res) {
            session('check',array('time'=>time()+300,'checkcode'=>$CheckCode,'tel'=>$tel));
            $this->ajaxReturn(array('reg'=>1),'json');
        } else {
            $this->ajaxReturn(array('reg'=>2),'json');
        }
    }

    /**
     * 检验验证码
     */
    public function check()
    {
        $check = I('checkcode');

        if (time() > session('check.time')) {
            $this->ajaxReturn(array('info'=>'验证码已过期!','reg'=>3),'json');
        } else {
            if ($check !== session('check.checkcode')) {
                $this->ajaxReturn(array('info'=>'验证码错误!','reg'=>2),'json');
            } else {
                $treemember = M('treemember');
                //openid入库
                $info = $treemember->where(array('tel'=>session('check.tel')))->find();
                if ($info['openid_1'] == null) {
                    $res1 = $treemember->data(array('openid_1'=>session('openid')))->where(array('tel'=>session('check.tel')))->save();
                    if ($res1 !== false){
                        $this->ajaxReturn(array('info'=>'绑定成功!','reg'=>1),'json');
                    }
                }
                if ($info['openid_2'] == null) {
                    $res2 = $treemember->data(array('openid_2'=>session('openid')))->where(array('tel'=>session('check.tel')))->save();
                    if ($res2 !== false){
                        $this->ajaxReturn(array('info'=>'绑定成功!','reg'=>1),'json');
                    }
                }
                if ($info['openid_3'] == null) {
                    $res3 = $treemember->data(array('openid_3'=>session('openid')))->where(array('tel'=>session('check.tel')))->save();
                    if ($res3 !== false){
                        $this->ajaxReturn(array('info'=>'绑定成功!','reg'=>1),'json');
                    }
                }
            }
        }

    }
    /**
     * @param $to 手机号
     * @param $datas 数据 数组
     * @param $tempId 模板ID
     * @return bool
     */
    protected function sendTemplateSMS($to,$datas,$tempId)
    {
        return true;
        // 初始化REST SDK
        $accountSid= 'aaf98f894fba2cb2014fca8732a80b94';
        //主帐号Token
        $accountToken= '2bdfffe0625145bea227341404fca865';
        //应用Id
        $appId='8a48b5514fba2f87014fca8a067e224a';
        //请求地址，格式如下，不需要写https://
        $serverIP='sandboxapp.cloopen.com';
        //请求端口
        $serverPort='8883';
        //REST版本号
        $softVersion='2013-12-26';
        vendor('SDK.CCPRestSDK');
        $rest = new \REST($serverIP,$serverPort,$softVersion);
        $rest->setAccount($accountSid,$accountToken);
        $rest->setAppId($appId);

        // 发送模板短信
        echo "Sending TemplateSMS to $to <br/>";
        $result = $rest->sendTemplateSMS($to,$datas,$tempId);
        if($result == NULL ) {
            echo "result error!";
            break;
        }
        if($result->statusCode!=0) {
            return false;
        }else{
            return true;

            //TODO 添加成功处理逻辑
        }
    }

    /**
     * 发送语音验证码
     */
    /**
     * 语音验证码
     * @param verifyCode 验证码内容，为数字和英文字母，不区分大小写，长度4-8位
     * @param playTimes 播放次数，1－3次
     * @param to 接收号码
     * @param displayNum 显示的主叫号码
     * @param respUrl 语音验证码状态通知回调地址，云通讯平台将向该Url地址发送呼叫结果通知
     * @param lang 语言类型。取值en（英文）、zh（中文），默认值zh。
     * @param userData 第三方私有数据
     */
    protected function sendVoiceSMS($verifyCode,$playTimes,$to,$displayNum,$respUrl,$lang,$userData)
    {
        $accountSid= 'aaf98f894fba2cb2014fca8732a80b94';

        //主帐号Token
        $accountToken= '2bdfffe0625145bea227341404fca865';

        //应用Id
        $appId='8a48b5514fba2f87014fca8b943a2256';

        //请求地址，格式如下，不需要写https://
        $serverIP='sandboxapp.cloopen.com';

        //请求端口
        $serverPort='8883';

        //REST版本号
        $softVersion='2013-12-26';
        // 初始化REST SDK
        vendor('SDK.CCPRestSDK');
        $rest = new \REST($serverIP,$serverPort,$softVersion);
        $rest->setAccount($accountSid,$accountToken);
        $rest->setAppId($appId);

        //调用语音验证码接口
        echo "Try to make a voiceverify,called is $to <br/>";
        $result = $rest->voiceVerify($verifyCode,$playTimes,$to,$displayNum,$respUrl,$lang,$userData);
        if($result == NULL ) {
            echo "result error!";
            break;
        }

        if($result->statusCode!=0) {
            return false;
            //TODO 添加错误处理逻辑
        } else{
            echo "voiceverify success!<br>";
            // 获取返回信息
            return true;
            // TODO 添加成功处理逻辑
        }
    }
    /**
     * 投诉建议
     */
    public function complaint()
    {
        if (IS_POST){
            $complaint = M('complaint');
            $data = $_POST;
            $data['createtime'] = time();
            $res = $complaint->data($data)->add();
            if ($res === false) {
                $this->error('反馈失败,请联系管理员');
            } else {
                $this->success('我们已收到您的反馈!');
            }
        } else {
            $this->display();
        }
    }
}